package com.galudisu.client

import akka.actor.{Actor, ActorLogging, ActorSystem, Props}
import akka.cluster.client.ClusterClientReceptionist
import com.galudisu.comm.LogarithmCalcJob
import com.typesafe.config.ConfigFactory

/**
  * @author galudisu
  */
class ClusterServer extends Actor with ActorLogging {

  override def receive: Receive = {

    case job: LogarithmCalcJob => sender() ! calc(job)
    case _ => None
  }

  def calc(job: LogarithmCalcJob) = {
    val radians = scala.math.toRadians(job.e)
    val result = scala.math.sin(radians)
    log info s"sine of an angle ${job.e } is ${BigDecimal(result).setScale(2, BigDecimal.RoundingMode.HALF_UP) }"
  }
}

object ClusterServer extends App {

  val config = ConfigFactory.load("server")
  val system = ActorSystem("ClusterSystem", config)

  val server = system.actorOf(Props[ClusterServer], name = "server")
  // 注册用于客户端
  ClusterClientReceptionist(system).registerService(server)

}
